package com.aduerr.v1shouyin.util;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * 作者：zhaoyang on 2017/10/15 10:47
 * 根据当前的时间获取昨天的开始时间和结束时间
 * 还有获取最近7天的开始时间和结束时间
 * 还有最近一个月的开始时间和结束时间
 */

public class DayWeekMoonTImeUtil {

    public static String getTodayBeginTime() {
        java.text.SimpleDateFormat sdFormatter = new java.text.SimpleDateFormat("yyyy" +
                "-MM-dd");
        Date curDate = new Date(System.currentTimeMillis());
        String str = sdFormatter.format(curDate);
        return str + " 00:00:00";
    }

    public static String getTodayEndTime() {
        java.text.SimpleDateFormat sdFormatter = new java.text.SimpleDateFormat("yyyy" +
                "-MM-dd");
        Date curDate = new Date(System.currentTimeMillis());
        String str = sdFormatter.format(curDate);
        return str + " 23:59:59";
    }

    public static String getYestdayBeginTime() {
        Date date = new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, -1);//把日期往前减少一天，若想把日期向后推一天则将负数改为正数
        date = calendar.getTime();
        java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM" +
                "-dd");
        String dateString = formatter.format(date);
        return dateString + " 00:00:00";
    }

    public static String getYesdayEndTime() {
        String yestdayBeginTime = getYestdayBeginTime();
        String[] split = yestdayBeginTime.split(" ");
        return split[0] + " 23:59:59";
    }

    public static String getLastWeekBeginTime() {
        Date date = new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        calendar.add(calendar.DATE, -7);//把日期往前减少一天，若想把日期向后推一天则将负数改为正数
        date = calendar.getTime();
        java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM" +
                "-dd");
        String dateString = formatter.format(date);
        return dateString + " 00:00:00";
    }

    public static String getLastWeekEndTime() {
//        String LastWeekEndTime = getLastWeekBeginTime();
//        String[] split = LastWeekEndTime.split(" ");

        return getYesdayEndTime();
    }

    public static String getLastMoonBeginTime() {
        Date date = new Date();//取时间
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date);
        //把日期往前减少一个月，若想把日期向后推一月 则将负数改为正数
        calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) - 1);
        date = calendar.getTime();
        java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("yyyy-MM");
        String dateString = formatter.format(date);
        return dateString + "-01 00:00:00";
    }

    //上月结束的时间
    public static String getLastMoonEndTime() {
        Calendar c = Calendar.getInstance();
        c.add(Calendar.MONTH, -1);
        java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd " +
                "HH:mm:ss");
        int lastMonthMaxDay = c.getActualMaximum(Calendar.DAY_OF_MONTH);
        c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), lastMonthMaxDay, 23, 59, 59);
        //按格式输出
        String gtime = sdf.format(c.getTime()); //上月最后一天
        return gtime;
    }

    //本月开始的时间
    public static String getThisMoonBeginTime() {
        java.text.SimpleDateFormat sdFormatter = new java.text.SimpleDateFormat("yyyy" +
                "-MM");
        Date curDate = new Date(System.currentTimeMillis());
        String str = sdFormatter.format(curDate);
        return str + "-01 00:00:00";
    }

    //本月结束的时间
    public static String getThisMoonEndTime() {
        String todayEndTime = getTodayEndTime();
        return todayEndTime;
    }

    /**
     * 获取当前时间字符串
     * <p>格式为yyyy-MM-dd HH:mm:ss</p>
     *
     * @return 时间字符串
     */
    public static String getNowTimeString() {
        return millis2String(System.currentTimeMillis(), "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 将时间戳转为时间字符串
     * <p>格式为pattern</p>
     *
     * @param millis  毫秒时间戳
     * @param pattern 时间格式
     * @return 时间字符串
     */
    public static String millis2String(long millis, String pattern) {
        return new SimpleDateFormat(pattern, Locale.getDefault()).format(new Date(millis));
    }

}
